package com.signway.javaui.slice.component;

import com.signway.javaui.ComponentAbility;
import com.signway.javaui.ResourceTable;
import com.signway.javaui.base.BaseAbilitySlice;
import com.signway.javaui.define.CommonData;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import java.util.ArrayList;
import java.util.List;

public class ImageComponent extends BaseAbilitySlice {

    List<Integer> imageList = new ArrayList<>();
    int index = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_component_image);

        imageList.add(ResourceTable.Media_image_1);
        imageList.add(ResourceTable.Media_image_2);

        initView();
    }

    private void initView() {
        Button button = (Button) findComponentById(ResourceTable.Id_switch_next_image);
        Image image = (Image) findComponentById(ResourceTable.Id_image);
        button.setClickedListener(component -> image.setImageAndDecodeBounds(imageList.get((++index) % 2)));

        Button startImageShow = (Button) findComponentById(ResourceTable.Id_open_image_show);
        startImageShow.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName(CommonData.COMPONENT_ABILITY)
                    .withAction(CommonData.CAT_IMAGE)
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
    }
}
